<template>
	<view class="home">
		<!-- 自定义组件 -->
		<navbar class="navbar"></navbar>
		<tab :list="tabList" @tab="tab" :tabIndex="tabIndex"></tab>
		<view class="home-list">
			<list :tablist = "tabList" @change="change" :activeIndex="activeIndex"></list>
		</view>
	</view>
</template>

<script>
	//easyCom componentes/组件名/组件名/vue  这样注册的组件可以不用引入也可以使用 局部引入
	import {mapState} from 'vuex'
	import navbar from '@/components/navbar/navbar.vue'
	import tab from '@/components/tab/tab.vue'
	import list from '@/components/list/list.vue'
	export default {
		components: {
			navbar,
			tab,
			list
		},
		computed:{
			...mapState(['userinfo'])
		},
		watch:{
			userinfo(newVal){
					this.getlabel()
			}
		},
		data() {
			return {
				title: 'Hello',
				tabList: [],
				tabIndex:0,
				activeIndex:0
			}
		},
		onLoad() {
			uni.$on('labelChange',(res)=>{
				//置空，防止数据重复加载导致key重复 1
				this.tablist = [];
				this.tabIndex = 0;
				this.activeIndex = 0;
				this.getlabel()
			})
			this.getlabel()
		},
		methods: {
			tab(data) {
				//发送的对象直接接受，只有data
				// console.log(data)
				this.activeIndex = data.index  
			},
			getlabel() {
				//调用云函数方法
				this.$api.get_label().then((res) => {
					const {
						data
					} = res;
					this.tabList = data
					// console.log(this.tabList)
					data.unshift({
						name:'全部'
					})
				})
			},
			change(current){
				this.tabIndex = current;
				// 选项卡切换后 控制上拉的noMore
				this.activeIndex = current
			}
		}
	}
</script>

<style lang = 'scss'>
	page{
		height: 100%;
		display: flex;
	}
	.home{
		display: flex;
		flex-direction: column;
		flex: 1;
		overflow: hidden;
		//调整卡片内容的高度
		.home-list{
			flex: 1;
			box-sizing: border-box;
		}
	}

</style>
